pro mksfh_event, ev
common sfh, status, t, sfr
common sfh_name, sfh_final_name
widget_control, ev.id, get_uvalue=uvalue

case uvalue of
'name':begin
	widget_control, ev.id, get_value=value
	status.outname=value
	end
'x':begin
	widget_control, ev.id, get_value=value
	status.x=value
    end
'y':begin
        widget_control, ev.id, get_value=value
	status.y=value
    end
'add_pt':begin
	  if keyword_set(t) then begin
		t=[t, float(status.x)]
		sfr=[sfr, float(status.y)]
	  endif else begin
	 	t=float(status.x)
	 	sfr=float(status.y)
	  endelse
	end
'ymax':begin
	widget_control, ev.id, get_value=value
	status.yr[1]=value
	end
'ymin':begin
        widget_control, ev.id, get_value=value
        status.yr[0]=value
        end
'draw':begin
	   converted=convert_coord(ev.x, ev.y, 0, /device, /to_data)
	   x=converted[0]
	   y=converted[1]
	if abs(ev.clicks) EQ 1 AND ev.type EQ 7 then begin
	  status.yr[1]*=1.2^ev.clicks
	endif
	if (ev.release AND 1) NE 0 then begin
	  if keyword_set(t) then begin
                t=[t, x]
                sfr=[sfr, y]
          endif else begin
                t=x
                sfr=y
          endelse
	endif
	;print, ev.ch EQ 68, ev.ch EQ 100, keyword_set(t)
	if (ev.ch EQ 68 or ev.ch EQ 100) AND keyword_set(t) then begin ;if d or D
	   ;convert to normal coordinates
	   cc=convert_coord(t, sfr, 0, /data, /to_normal)
	   cli=convert_coord(ev.x, ev.y, 0, /device, /to_normal)
	   dists=sqrt((cc[0,*]-cli[0])^2+(cc[1,*]-cli[1])^2)
	   min1=min(dists, min_ind)
	   if min1 LT .05 then begin
		if n_elements(t) EQ 1 then begin
		   delvarx, t
		   delvarx, sfr
		endif else $
		if min_ind EQ n_elements(t)-1 then begin
		   t=t[0:min_ind-1]
		   sfr=sfr[0:min_ind-1]
		endif else $
		if min_ind EQ 0 then begin
                   t=t[1:*]
                   sfr=sfr[1:*]
                endif else begin
                   t=[t[0:min_ind-1], t[min_ind+1:*]]
                   sfr=[sfr[0:min_ind-1], sfr[min_ind+1:*]]
                endelse
	   endif
	endif

	if (ev.press AND 4) NE 0 AND keyword_set(t) then begin
	   cc=convert_coord(t, sfr, 0, /data, /to_normal)
           cli=convert_coord(ev.x, ev.y, 0, /device, /to_normal)
           dists=sqrt((cc[0,*]-cli[0])^2+(cc[1,*]-cli[1])^2)
           min1=min(dists, min_ind)
           if min1 LT .05 then begin
		status.ind_move=min_ind
	   endif
	endif
	if status.ind_move GE 0 and (ev.release AND 4) NE 0 then begin
	   converted=convert_coord(ev.x, ev.y, 0, /device, /to_data)
	   t[status.ind_move]=converted[0]
	   sfr[status.ind_move]=converted[1]
	   status.ind_move=-1
	endif

	end
'load':begin
	res=rds_fileselect(status.tlb, filter='*sfh*')
	if keyword_set(res) then readcol, res, t, sfr
	end
'clear':begin
	delvarx, t
	delvarx, sfr
	end
else:
endcase



plot_again
if strmatch(uvalue, 'ok') then begin
	spawn, 'echo $SLUG_DIR', dir
	openw, lun, dir+'/output/'+status.outname, /get_lun
	printf, lun, 'BEGIN'
	printf, lun, [0],[0]
	for i=0, n_elements(t)-1 do printf, lun, t[i]*1d6, sfr[i]
	printf, lun, 'END'
	close, lun
	sfh_final_name=status.outname
	widget_control, ev.top, /destroy
endif
if strmatch(uvalue, 'quit') then begin
	 status.outname=''
  	 sfh_final_name='bad_exit_123456'
	 widget_control, ev.top, /destroy
endif
end

;-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
pro plot_again
common sfh, status, t, sfr
if keyword_set(t) then begin
  isort=sort(t)
  t=t[isort]
  sfr=sfr[isort]
endif

widget_control, status.ymin, set_value=rstring(status.yr[0])
widget_control, status.ymax, set_value=rstring(status.yr[1])
wset, status.winid

plot, [0], [0], xtitle='Time [Myr]', $
	ytitle='SFR [Msun/yr]', xr=[0, status.maxtime/1d6], $
	yr=status.yr, /xsty, /ysty, /nodata
;oplot,[(keyword_set(t)?t:0)], [keyword_set(t)?sfr:0], color=fsc_color('BLU4')
;oplot,[(keyword_set(t)?t:0)], [keyword_set(t)?sfr:0], color=fsc_color('ORG4'), ps=symcat(16)
if keyword_set(t) AND keyword_set(sfr) then begin
nt=n_elements(t)
tsav=[0, 0, t[reform(lindgen(nt)##replicate(1, 2), 2*nt)]]
sfrsav=[0, sfr[reform(lindgen(nt)##replicate(1, 2), 2*nt)], 0]
oplot, tsav, sfrsav, color=fsc_color('BLU4')
oplot, t, sfr, color=cgcolor('ORG4'),ps=symcat(16)
endif

if status.ind_move GE 0 then $
	oplot, [t[status.ind_move]], [sfr[status.ind_move]], color=fsc_color('RED'),$
		ps=symcat(22), symsize=3

end


;-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

PRO mksfh, base, maxtime
common sfh, status, t, sfr
if keyword_set(base) then begin
widget_control, base, tlb_get_offset=offs, tlb_get_size=sizes
  if keyword_set(x) EQ 0 then x=offs[0]+sizes[0]/2.
  if keyword_set(y) EQ 0 then y=offs[1]+sizes[1]/2.
end


status={tlb:0L, outname:'my.sfh', winid:0L, list:0L,$
	 maxtime:maxtime, x:'', y:'', yr:[0.,1d], $
	ymin:0L, ymax:0L, ind_move:-1L}

status.tlb=widget_base(/col, xs=650, ys=400)
row00=widget_base(status.tlb, /row)
lbl=widget_label(row00, value='File Name:')
name=widget_text(row00, /editable, /all, uvalue='name', ysize=1, value='my.sfh')
btn=widget_button(row00, value='Load', uvalue='load')
btn=widget_button(row00, value='Clear', uvalue='clear')

col0=widget_base(row00, /row, /frame)
row0=widget_base(col0, /row)
lbl=widget_label(row0, value='ymax [Msun/yr]:')
status.ymax=widget_text(row0, /editable, /all, uvalue='ymax', $
		xsize=6)
row1=widget_base(col0, /row)
lbl=widget_label(row1, value='ymin [Msun/yr]:')
status.ymin=widget_text(row1, /editable, /all, uvalue='ymin', $
	xsize=6)




;row00=widget_base(status.tlb, /row, /align_center)

draw=widget_draw(status.tlb, scr_ysize=250, scr_xsize=650., $
		/wheel, /button, /key, uvalue='draw')
row11=widget_base(status.tlb, /row)
lbl=widget_label(row11, value='(time [Myr], SFR [Msun/yr]) = (')
name=widget_text(row11, /editable, /all, uvalue='x')
lbl=widget_label(row11, value=',')
name=widget_text(row11, /editable, /all, uvalue='y')
lbl=widget_label(row11, value=')')

add_pt=widget_button(row11, value='Add Pt', uvalue='add_pt')
row00=widget_base(status.tlb, /row, /align_center)
lbl2=widget_label(row00, value='              ')
quitbtn=widget_button(row00, value='Cancel', uvalue='quit')
okbtn=widget_button(row00, value='OK', uvalue='ok')

widget_control, status.tlb, /realize
widget_control, draw, get_value=value
status.winid=value
widget_control, status.tlb, tlb_get_size=tlbsize
if keyword_set(x) then $
	centertlb, status.tlb, x-tlbsize[0]/2, y-tlbsize[1]/2, /nocenter, /device $
	else centertlb, status.tlb
plot_again
xmanager, 'mksfh', status.tlb, /no_block

end
